package com.wisemoresystem.seal.action.role;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.config.Config;
import com.wisemoresystem.seal.config.FileFolderTools;
import com.wisemoresystem.seal.model.t_file;
import com.wisemoresystem.seal.model.t_location;
import com.wisemoresystem.seal.model.t_role;
import com.wisemoresystem.seal.service.LocationService;
import com.wisemoresystem.seal.service.MenuService;
import com.wisemoresystem.seal.service.RoleService;
import com.wisemoresystem.seal.util.ExcelReadToolForList;
import com.wisemoresystem.seal.util.doc.excel.ExcelExport;
import com.wisemoresystem.seal.util.doc.excel.ExcelReadTools;
import com.wisemoresystem.seal.util.json.JsonTools;

/**
 * 角色管理
 * 
 * @author Administrator
 *
 */
@Controller("ixRole")
@Scope("prototype")
@RequestMapping("/ix/role")
public class RoleAction extends ProjBaseAction {

	@Autowired
	private LocationService lService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private MenuService menuService;

	/**
	 * 角色列表
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {

		List<Map<String, Object>> list = roleService.selectRoleList(null);

		ModelAndView mv = new ModelAndView("/jsp/role/list");
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/add_form")
	public ModelAndView add_form() {

		List<Map<String, Object>> menuList = menuService.selectMenuList();
		String menuJson = JsonTools.toJson(menuList);

		List<Map<String, Object>> bankList = roleService.selectLevelList();
		String bankJson = JsonTools.toJson(bankList);

		ModelAndView mv = new ModelAndView("/jsp/role/add_form");
		mv.addObject("menuJson", menuJson);
		mv.addObject("bankJson", bankJson);
		return mv;
	}

	/**
	 * 角色添加
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_role role) {

		roleService.saveObj(role);
		logT.logH("添加角色", t_role.class, role.get_id());

		super.setTipMsg("保存成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 修改的角色信息
	 * 
	 * @param role_id
	 *            角色id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String role_id) {

		List<Map<String, Object>> menuList = menuService.selectMenuList();
		String menuJson = JsonTools.toJson(menuList);

		List<Map<String, Object>> bankList = roleService.selectLevelList();
		String bankJson = JsonTools.toJson(bankList);
		t_role role = oc.findObjById(role_id, t_role.class);

		ModelAndView mv = new ModelAndView("/jsp/role/edit_form");
		mv.addObject("role", role);
		mv.addObject("menuJson", menuJson);
		mv.addObject("bankJson", bankJson);
		return mv;
	}

	/**
	 * 角色修改
	 * 
	 * @param role_id
	 *            角色id
	 * @param role
	 *            角色信息
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(String role_id, t_role role) {

		t_role roleDb = oc.findObjById(role_id, t_role.class);
		roleDb.setAuth_codes(role.getAuth_codes());
		roleDb.setAuth_super(role.getAuth_super());
		roleDb.setName(role.getName());
		roleDb.setNote(role.getNote());
		roleDb.setSort(role.getSort());

		roleService.updateObj(roleDb);

		logT.logH("修改角色", t_role.class, role.get_id());

		super.setTipMsg("修改成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 删除角色
	 * 
	 * @param role_id
	 *            角色 id
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView del(String role_id) {
		// t_role role = roleService.selectRoleById(role_id);
		oc.delObjById(role_id, t_role.class);

		logT.logH("删除角色", t_role.class, role_id);

		super.setTipMsg("删除成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 区域模板
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/export_m")
	public ModelAndView exportMo() throws IOException {
		String tit = "中国区域及其编码";

		String[] arrays = { "区域编码", "地区名称", "所属区域编码" };

		ExcelExport<t_location> ts = new ExcelExport<t_location>();
		String filename = System.currentTimeMillis() + ".xls";

		File file = new File(FileFolderTools.getExportFolder(), filename);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		OutputStream os = null;
		try {
			os = new FileOutputStream(file);
			ts.exportExcelL(tit, "中国区域及其编码", arrays, os);
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				os.close();
			}
		}
		ModelAndView mv = new ModelAndView("redirect:"
				+ FileFolderTools.getExportUrl() + filename);
		return mv;
	}

	// //工资表
	// @RequestMapping("/export_c")
	// public ModelAndView export(String enterprise_id, String month) throws
	// IOException {
	// String tit = "中国区域及其编码";
	//
	// String[] arrays = {"用工号","账号", "身份证号", "姓名", "应发工资", "养老保险扣款","失业保险扣款",
	// "医疗保险扣款", "大病扣款", "公积金扣款", "其他扣款", "个调税", "扣款合计", "实发工资"};
	//
	// List<Map<String, Object>> list = lService.downloadMonthSalaryList();
	//
	// ExcelExport<t_location> ts = new ExcelExport<t_location>();
	//
	// String filename = System.currentTimeMillis() + ".xls";
	// File file = new File(FileFolderTools.getExportFolder(), filename);
	// if (!file.getParentFile().exists()) {
	// file.mkdirs();
	// }
	// OutputStream os = null;
	// try {
	// os = new FileOutputStream(file);
	// ts.exportExcel(tit,"工资表单", arrays, list, os);
	// os.flush();
	// } catch (IOException e) {
	// e.printStackTrace();
	// } finally {
	// if (os != null) {
	// os.close();
	// }
	// }
	// ModelAndView mv = new ModelAndView("redirect:"
	// + FileFolderTools.getExportUrl() + filename);
	// return mv;
	// }
	/**
	 * 表格测试
	 * 
	 * @param file_id
	 *            问价id
	 * @throws IOException
	 */
	@RequestMapping("/import_c")
	public void importlinkmans(String file_id) throws IOException {
		resp.setCharacterEncoding("utf-8");

		t_file tf = lService.selectFileById(file_id);
		File file = new File(
				Config.DATA_PATH + File.separator + tf.getFolder(),
				tf.getNew_file_name());
		if (!file.exists()) {
			file = new File(Config.DATA_PATH, "file404.jpg");
		}

		InputStream is = null;
		try {
			is = new FileInputStream(file);
			// 对读取Excel表格标题测试
			ExcelReadTools excelReader = new ExcelReadTools();

			// 对读取Excel表格内容测试
			Map<Integer, String> map = excelReader.readExcelContent(is);
			String par = map.get(1);
			String[] obj = par.split("#");
			String[] content = { "code", "name", "super_code" };
			try {
				List<Map<String, Object>> list = ExcelReadToolForList
						.turnToList(content, map, obj.length);
				System.err.println(list.size() + "===" + list);
				try {
					lService.saveLocation(list);
				} catch (RuntimeException e1) {
					resp.getWriter().print("导入失败！");
					resp.getWriter().flush();
					return;
				}
			} catch (RuntimeException e) {
				resp.getWriter().print("导入文件不符合！");
				resp.getWriter().flush();
				return;
			}

			resp.getWriter().print("导入成功");
			resp.getWriter().flush();
		} catch (FileNotFoundException e) {
			// System.out.println("未找到指定路径的文件!");
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
